/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.awt.event.ActionEvent;
import java.lang.reflect.Method;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import org.balisunrise.annotation.EventType;
import org.balisunrise.common.Event;
import org.balisunrise.common.abstracts.AbstractVisualizable;
import org.balisunrise.swing.transaction.events.VisualActionEvent;

/**
 *
 * @author Glauco
 */
public class VisualAction extends AbstractVisualizable
        implements org.balisunrise.common.VisualAction<JButton>{

    Object map;
    Method method;
    JButton button;
    Action action;
    boolean withParm;

    public void perform(Event evt) {
        try{
            if(withParm)
                method.invoke(map, evt);
            else
                method.invoke(map);
        } catch(Exception ex){

            String msg = "Ação não pode ser disparada.\n" +
                    ex.getCause() + ex.getMessage() +
                    "\nMAP: " + map.toString()+
                    "\nMETHOD: " + method.toString() +
                    "\nACTION: " + action.toString();

            System.out.println(msg);
        }
    }

    /**
     * Return always true.
     * @return true.
     */
    public boolean verify(Class entityClass, EventType eventType, String alias,
            String property) {
        return true;
    }

    public JButton getVisualRepresentation() {
        return button;
    }

    public void createAction(String text, Icon icon){

        AbstractAction aa = new AbstractAction(text, icon) {
            public void actionPerformed(ActionEvent e) {
                try{
                    perform(new VisualActionEvent(VisualAction.this));
                } catch(Exception ex){}
            }
        };
        
        this.setAction(aa);
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {

        this.action = action;

        if (getVisualRepresentation() != null)
            getVisualRepresentation().setAction(action);
    }

    public JButton getButton() {
        return button;
    }

    public void setButton(JButton button) {
        this.button = button;
    }

    public Object getMap() {
        return map;
    }

    public void setMap(Object map) {
        this.map = map;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }
}
